package com.example.wechat.wechat;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.*;
import android.view.View;
import android.widget.*;
import java.util.*;

public class Chat extends AppCompatActivity {

    private List<Massage> massageList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView recycleView;
    private MassageAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initMassages();
        inputText =(EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        recycleView = (RecyclerView)findViewById(R.id.msg_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter =  new MassageAdapter(massageList);
        recycleView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
                if (!"".equals(content)){
                    Massage massage = new Massage(content,R.drawable.contact_pressed,Massage.TYPE_SENT);
                    massageList.add(massage);
                    adapter.notifyItemInserted(massageList.size() - 1);//当有新消息时，刷新RecyclerView中的显示
                    recycleView.scrollToPosition(massageList.size() - 1);//将RecyclerView定位到最后一行
                    inputText.setText("");//清空输入框中的内容
                }
            }
        });

    }

    private void initMassages(){
        Massage massage1 = new Massage("Hi,guy" ,  R.drawable.contact_pressed,Massage.TYPE_RECEIVED);
        massageList.add(massage1);
        Massage massage2 = new Massage("hello .Who is that??" ,  R.drawable.contact_pressed,Massage.TYPE_SENT);
        massageList.add(massage2);

    }
}
